package mobility;

import java.sql.ResultSet;
import java.sql.SQLException;

public class zonas {

	private db conexion = null;
	private String codigo;
	private String descripcion;
	private String estado;
	private String error;
	private String key;
	
	public String getEstado() {
		return estado;
	}
	public void setEstado(String estado) {
		this.estado = estado;
	}
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public String getError() {
		return error;
	}
	public void setError(String error) {
		this.error = error;
	}
	public zonas() {
		this.codigo = "";
		this.descripcion = "";
	}
	public zonas(db cnt) {
		this.conexion = cnt;
		this.codigo = "";
		this.descripcion = "";
	}
	
	public db getConexion() {
		return conexion;
	}
	public void setConexion(db conexion) {
		this.conexion = conexion;
	}
	public String getCodigo() {
		return codigo;
	}
	public void setCodigo(String codigo) {
		this.codigo = codigo;
	}
	public String getDescripcion() {
		return descripcion;
	}
	public void setDescripcion(String descripcion) {
		this.descripcion = descripcion;
	}
	
	public boolean add(){
		boolean result = false;
		try {
			String sql = String.format ("insert into zonas (zonacodi,zonadesc,zonaesta) values ('%s','%s',%s)",this.codigo,this.descripcion,this.estado);
			if (conexion.Update(sql) > 0) {
				conexion.Commit();
				result = true;
			}
			
		}catch (SQLException ex) {
			this.error = ex.getMessage();
		}
		return result;
	}
	public boolean modify() {
		boolean result = false;
		try {
			String sql = String.format ("update zonas set zonacodi='%s',zonadesc='%s',zonaesta='%s' where zonacodi='%s'",this.codigo,this.descripcion,this.estado,this.key);
			if (conexion.Update(sql) > 0) {
				conexion.Commit();
				result = true;
			}
			
		}catch (SQLException ex) {
			this.error = ex.getMessage();
		}
		return result;	
	}
	
	public boolean remove() {
		boolean result = false;
		try {
			String sql = String.format("delete from zonas where zonacodi='%s'",this.key);
			if (conexion.Update(sql) > 0) {
				conexion.Commit();
				result = true;
			}
			
		}catch (SQLException ex) {
			this.error = ex.getMessage();
		}
		return result;	
	}
	
	public boolean exist() {
		boolean result = false;
		try {
			String sql = "select zonacodi,zonadesc,zonaesta from zonas where zonacodi='" + this.key + "'";
			ResultSet rs = conexion.Query(sql);
			if (rs.next()) {
				this.codigo = (String)rs.getString("zonacodi");
				this.descripcion = (String)rs.getString("zonadesc");
				this.estado = (String)rs.getString("zonaesta");
				result = true;
			}
			rs.close();
			
		}catch (SQLException ex) {
			this.error = ex.getMessage();
		}
		return result;	
	}
	public ResultSet list() {
		ResultSet result = null;
		try {
			String sql = "select zonacodi,zonadesc from zonas order by zonadesc";
			result = conexion.Query(sql);
		}catch (SQLException ex) {
			this.error = ex.getMessage();
		}
		return result;	
	}
	public String CreateSelectHTML(String id) {
		String strHtml = "<select id='" + id + "' name='" + id + "'>" ;
		ResultSet rs = list();
		try {
			if (rs.next()) {
				do {
				strHtml += "<option value='" + rs.getString("zonacodi") + "'>" + rs.getString("zonadesc") + "</option>";
				}while(rs.next());
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			this.error = e.getMessage();
		}
		
		strHtml += "<select>";
		return strHtml;
	}
	public String CreateSelectHTML(String id,String key) {
		String strHtml = "<select id='" + id + "' name='" + id + "'>" ;
		ResultSet rs = list();
		try {
			if (rs.next()) {
				do {
					String c = "";
					if (key.equals(rs.getString("zonacodi"))) c="selected";
					
				strHtml += "<option value='" + rs.getString("zonacodi") + "' " + c + ">" + rs.getString("zonadesc") + "</option>";
				}while(rs.next());
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			this.error = e.getMessage();
		}
		
		strHtml += "<select>";
		return strHtml;
	}
	public String CreateSelectHTML(String id,String key,String filtro) {
		String strHtml = "<select id='" + id + "' name='" + id + "'>" ;
		String sql = "select zonacodi,zonadesc from zonas ";
		if (!filtro.equals("")) {
			sql += " where " + filtro ;
		}
		sql+= " order by zonadesc";
		
		try {
			ResultSet rs = conexion.Query(sql);
			if (rs.next()) {
				do {
					String c = "";
					if (key.equals(rs.getString("zonacodi"))) c="selected";
					
				strHtml += "<option value='" + rs.getString("zonacodi") + "' " + c + ">" + rs.getString("zonadesc") + "</option>";
				}while(rs.next());
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			this.error = e.getMessage();
		}
		
		strHtml += "<select>";
		return strHtml;
	}
}
